#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    int kthToLast(ListNode* head, int k) {
        int i = 0;
        return kLast(head, k, i);
    }
    int kLast(ListNode* node, int& k, int& i)
    {
        if (!node->next)
        {
            ++i;
            return node->val;
        }
        int v = kLast(node->next, k, i);
        ++i;
        if (i == k)
        {
            return node->val;
        }
        //cout << i << " ";

        return v;
    }
};